package com.wbdgj.ui.store;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.mine.PointShopActivity;
import com.wbdgj.ui.mine.Shop01Fragment;
import com.wbdgj.ui.store.fragment.PointsMall01Fragment;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

public class PointsMallActivity extends BaseActivity {
    private Context context = this;

    @BindView(R.id.destination_tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.destination_viewpager)
    ViewPager mViewPager;

    private MyFragmentPagerAdapter adapter;
    private List<Fragment> fragments;
    private PointsMall01Fragment pointsMall01Fragment;

    private ArrayList<LinkedTreeMap> linkedTreeMaps;
    private DecimalFormat decimalFormat = new DecimalFormat("###################.###########");

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_point_shop_detail;
    }

    @Override
    protected void initView() {
        getGoodsCategoryList();

        findViewById(R.id.text_top_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launch(MallRecordActivity.class);
            }
        });
    }
    private class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
            init();
        }
        @Override
        public Fragment getItem(int position) {
            return createFragment(PointsMall01Fragment.class,position);
        }

        protected Fragment createFragment (Class clazz, int position){
            Bundle bundle = new Bundle();
            bundle.putString("ID", decimalFormat.format(linkedTreeMaps.get(position).get("Id")));
            return  Fragment.instantiate(context, clazz.getName(), bundle);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return linkedTreeMaps.get(position).get("Name")+"";
        }

        @Override
        public int getCount() {
            return linkedTreeMaps.size();
        }
        private void init() {
        }
    }
    private void getGoodsCategoryList() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getGoodsCategoryList(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){

                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("data")){
                        linkedTreeMaps = new ArrayList<>();
                        linkedTreeMaps = (ArrayList<LinkedTreeMap>) linkedTreeMap.get("data");
                        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
                        mViewPager.setAdapter(adapter);
                        mTabLayout.setupWithViewPager(mViewPager);
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }
}
